package com.yksj.healthtalk.ui.home;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;

import com.yksj.healthtalk.comm.BaseFragmentActivity;
import com.yksj.healthtalk.net.http.AsyncHttpResponseHandler;
import com.yksj.healthtalk.net.http.HttpRestClient;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.utils.SystemUtils;
import com.yksj.healthtalk.utils.ToastUtil;
// 摄影馆 添加链接
public class LinkedSetFragmentActivity extends BaseFragmentActivity implements OnClickListener{
	
	private String shopId;
	private String shopLink;
	/**
	 * 表示edittext是否显示  false 为不显示
	 */
	private boolean flag = false;
	private EditText mEditText;
	private TextView mTextView;
	private boolean isSumit;//true表示修改过
//	private String regex ="http://(([a-zA-z0-9]|-){1,}\\.){1,}[a-zA-z0-9]{1,}-*";
	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.linked_set_layout);
		shopId =getIntent().getStringExtra("shopid");
		shopLink = getIntent().getStringExtra("shoplink");
		initView();
	}

	public void initView() {
		initTitle();
		titleLeftBtn.setOnClickListener(this);
		titleTextV.setText("设置链接");
		titleRightBtn.setVisibility(View.VISIBLE);
		titleRightBtn.setText("确定");
		titleRightBtn.setOnClickListener(this);
		mTextView = (TextView)findViewById(R.id.input_linked_text);
		mEditText = (EditText)findViewById(R.id.input_linked_edittext);
		mTextView.setOnClickListener(this);
		if(!TextUtils.isEmpty(shopLink)) {
			mTextView.setBackgroundResource(R.drawable.textview_background_up);
			mEditText.setVisibility(View.VISIBLE);
			mEditText.setText(shopLink);
			flag = true;
		}
	}
	
	private boolean isUrlPath(String str){
		if(str == null || str.length() == 0) return false;
		/*String regStr = "([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?";
		return  Pattern.compile(regStr).matcher(str).matches();*/
		return str.contains(".");
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()) {
		case R.id.title_back:
			SystemUtils.hideSoftBord(this, mEditText);
			if(isSumit){
				Intent intent = getIntent();
				intent.putExtra("shopLink", shopLink);
				setResult(RESULT_OK, intent);
				finish();
			}else{
				onBackPressed();	
			}
			break;
		case R.id.title_right:
			//如果shoplink为空 表示 网址输入框隐藏 则 直接返回不设置连接到服务器
			if(!flag) {
				//网址输入框隐藏 表示 用户清空输入的网址则 将网址返回为空
				shopLink = "";
			} else {
				shopLink = mEditText.getText().toString().trim();
			}
			if(isUrlPath(shopLink)){
				setLink(shopLink);
			}else{
				ToastUtil.showBasicShortToast(getApplicationContext(), "请输入有效的地址");
			}
			break;
		case R.id.input_linked_text:
			if(!flag) {
				mTextView.setBackgroundResource(R.drawable.textview_background_up);
				mEditText.setVisibility(View.VISIBLE);
				mEditText.setText("");
			} else {
				mTextView.setBackgroundResource(R.drawable.text_background_single);
				mEditText.setVisibility(View.INVISIBLE);
				shopLink = mEditText.getText().toString();
			}
			flag = !flag;
			break;
		}
	}
	
	public void setLink(final String content) {
		
			HttpRestClient.doHttpSetLink(shopId,content,new AsyncHttpResponseHandler(this) {
				@Override
				public void onSuccess(int statusCode, String response) {
					super.onSuccess(statusCode, response);
					if(response.equals("Y")) {
						isSumit=true;
						ToastUtil.showToastPanl("设置地址成功...");
						Intent intent = getIntent();
						intent.putExtra("shopLink", shopLink);
						setResult(RESULT_OK, intent);
						finish();
					} else {
						onFailure(null,null);
					}
				}
			});
		
	}
}
